<template>
  <div>
    <div class="wrapper">
    <img class="wrapper__img" src="http://www.dell-lee.com/imgs/vue3/user.png"></img>
    <div class="wrapper__input">
    <input type="text" 
    class="wrapper__input__content"
    placeholder="请输入用户名"
    v-model="user.username"
    >
    </div>
    <div class="wrapper__input">
    <input type="password" 
    class="wrapper__input__content"
    placeholder="请输入密码"
    v-model="user.password"
    >
    </div>
    <div class="wrapper__login__button" @click="handleLogin">登录</div>
    <router-link :to="{name:'Register'}">
    <div class="wrapper__login__link">立即注册</div>
    </router-link>
    <Toast :showMessage = "show.showMessage" v-if="show.showToast"/>
    </div>
  </div>
</template>

<script>
// import router from "../../router";
// import axios from "axios";
import { post } from "../../utils/utils";
// axios.defaults.headers.post["Content-Type"] = "application/json";
import Toast from "../../components/toast/Toast";
export default {
  name: "Login",
  components: {
    Toast,
  },
  data() {
    return {
      user: { username: "", password: "" },
      show: { showToast: false, showMessage: "" },
    };
  },
  methods: {
    //封装toast显示函数
    ShowToast(message) {
      this.show.showToast = true;
      this.show.showMessage = message;
      setTimeout(() => {
        this.show.showToast = false;
        this.show.showMessage = "";
      }, 2000);
    },

    //提交逻辑
    handleLogin() {
      if (!this.user.username.trim() || !this.user.password.trim()) {
        this.ShowToast("输入不能为空");
        return;
      }
      post("/api/user/login", {
        username: this.user.username,
        password: this.user.password,
      })
        .then(() => {
          localStorage.isLogin = true;
          this.$router.push({ name: "Home" });
        })
        .catch(() => {
          this.ShowToast("登陆失败");
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  margin: 0 auto;
  width: 2.95rem;
  transform: translateY(-50%);
  &__img {
    display: block;
    margin: 0 auto 0.4rem auto;
    width: 0.66rem;
    height: 0.66rem;
  }
  &__input {
    height: 0.24rem;
    background: #f9f9f9;
    border: 0.01rem solid rgba(0, 0, 0, 0.1);
    border-radius: 0.06rem;
    margin-bottom: 0.16rem;
    padding: 0.12rem 0.16rem;
    &__content {
      background: #f9f9f9;
      line-height: 0.24rem;
      outline: none;
      border: none;
      width: 100%;
      height: 100%;
      font-size: .16rem;
      color: rgba(0, 0, 0, 0.5);
      letter-spacing: 0;
      line-height: 0.24rem;
    }
  }
  &__login__button {
    background: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.04rem;
    height: 0.48rem;
    text-align: center;
    line-height: 0.48rem;
    font-family: PingFangSC-Regular;
    font-size: .16rem;
    color: #ffffff;
    letter-spacing: 0;
    margin-top: 0.32rem;
  }
  &__login__link {
    text-align: center;
    font-family: PingFangSC-Regular;
    font-size: 0.14rem;
    color: rgba(0, 0, 0, 0.5);
    letter-spacing: 0;
    margin-top: 0.16rem;
  }
}
</style>